//
//  RSInputViewController.m
//  ClientTax
//
//  Created by 侯伟 on 2020/1/13.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSInputViewController.h"

@interface RSInputViewController ()<UITextViewDelegate>

/// 标题
@property (nonatomic,strong) UILabel *titleLb;

/// 内容
@property (nonatomic,strong) UITextView *textView;

/// 占位文本
@property (nonatomic,strong) UILabel *placeholderLb;

/// 输入计数
@property (nonatomic,strong) UILabel *countLb;

@end

@implementation RSInputViewController

- (void)loadView{
    [super loadView];
    [self initViews];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(_maxLenth == 0){
        _maxLenth = 300;
    }
    
    if(self.text){
        self.textView.text = self.text;
        [self textViewDidChange:self.textView];
    }

}

- (void)initViews{

    MyLinearLayout* popView = [[MyLinearLayout alloc] initWithOrientation:(MyOrientation_Vert)];
    popView.backgroundColor = UIColor.whiteColor;
    popView.myHorzMargin = PX(37.5);
    popView.myCenterY = -40;
    popView.subviewVSpace = 16;
    popView.myHeight = MyLayoutSize.wrap;
    [self.view addSubview:popView];
    self.popView = popView;
    
    WeakSelf
    popView.endLayoutBlock = ^{
        weakSelf.placeholderLb.frame = CGRectMake(CGRectGetMinX(weakSelf.textView.frame)+2, CGRectGetMinY(weakSelf.textView.frame)+2, CGRectGetWidth(weakSelf.textView.frame), 16);
        
        weakSelf.countLb.frame = CGRectMake(CGRectGetMaxX(weakSelf.textView.frame) - CGRectGetWidth(weakSelf.countLb.frame), CGRectGetMaxY(weakSelf.textView.frame)-CGRectGetHeight(weakSelf.countLb.frame), CGRectGetWidth(weakSelf.countLb.frame), CGRectGetHeight(weakSelf.countLb.frame));
    };
    
    
    UILabel* titleLb = [UILabel lableWithFont:FONTBOLD_FIT(15) color:COLOR_BLACK_DARK text:_titleStr];
    titleLb.lineBreakMode = NSLineBreakByWordWrapping;
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.myHorzMargin = 15.0;
    titleLb.myTop = 20;
    titleLb.myHeight = MyLayoutSize.wrap;
    [popView addSubview:titleLb];
    _titleLb = titleLb;
    
    UITextView *textView = [UITextView new];
    textView.textColor = COLOR_BLACK_DARK;
    textView.font = FONT_FIT(15);
    textView.tintColor = COLOR_THEME;
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.returnKeyType = UIReturnKeyDone;
    textView.showsVerticalScrollIndicator = NO;
    textView.showsHorizontalScrollIndicator = NO;
    textView.textContainerInset = UIEdgeInsetsZero;
    textView.textContainer.lineFragmentPadding = 0;
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textView.myHorzMargin = 15.0;
    textView.heightSize.equalTo(@(MyLayoutSize.wrap)).min(80);
    
    textView.layer.cornerRadius = 4.0;
    textView.layer.borderWidth = Size_1PX;
    textView.layer.borderColor = COLOR_GRAY_LINE.CGColor;
    
    [popView addSubview:textView];
    _textView = textView;
    
    UILabel* placeholderLb = [UILabel lableWithFont:FONT_FIT(14) color:COLOR_GRAY_LINE text:self.placeholder];
    placeholderLb.textAlignment = NSTextAlignmentLeft;
    placeholderLb.useFrame = YES;
    [popView addSubview:placeholderLb];
    _placeholderLb = placeholderLb;
    
    NSString* countString = [NSString  stringWithFormat:@"0/%ld",_maxLenth];
    UILabel* countLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_LINE text:countString];
    countLb.textAlignment = NSTextAlignmentRight;
    countLb.useFrame = YES;
    [popView addSubview:countLb];
    _countLb = countLb;
    
    
    MyBorderline* topLine = [MyBorderline new];
    topLine.thick = 1.0;
    topLine.color = COLOR_GRAY_LINE;
    
    MyLinearLayout* buttonLayout = [[MyLinearLayout alloc] initWithOrientation:(MyOrientation_Horz)];
    buttonLayout.gravity = MyGravity_Horz_Stretch;
    buttonLayout.topBorderline = topLine;
    buttonLayout.myHeight = PX(44.0);
    buttonLayout.myMargin = 0;
    [popView addSubview:buttonLayout];
    

    UIButton* cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:COLOR_BLACK_DARK forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = FONT_FIT(15);
    [cancelBtn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.myMargin = 0;
    [buttonLayout addSubview:cancelBtn];
    
    UIView* btnSpace = [UIView new];
    btnSpace.backgroundColor = COLOR_GRAY_LINE;
    btnSpace.myWidth = Size_1PX;
    btnSpace.myCenterY = 0;
    btnSpace.myMargin = 0;
    [buttonLayout addSubview:btnSpace];
    
    UIButton* doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneBtn setTitleColor:COLOR_THEME forState:UIControlStateNormal];
    doneBtn.titleLabel.font = FONT_FIT(15);
    [doneBtn addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
    doneBtn.myMargin = 0;
    [buttonLayout addSubview:doneBtn];
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType{
    _textView.keyboardType = keyboardType;
}

#pragma mark - actions

- (void)done:(UIButton *)sender {
    
    NSString* text = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (!text.length) {
        [Toast  toast:self.placeholder.length?self.placeholder:@"请输入"];
        return;
    }
    [self dismissAlertController:^{
        if(self.inputBlock){
            self.inputBlock(text);
        }
    }];
}
- (void)cancel:(UIButton *)sender {
    [self dismissAlertController:NULL];
}

#pragma mark -- UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    
    self.placeholderLb.hidden = textView.text.length > 0;
    [self.placeholderLb sizeToFit];
    
    NSString* text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    self.countLb.text = [NSString stringWithFormat:@"%ld/%ld",text.length,self.maxLenth];
    [self.countLb sizeToFit];
    
    //每次输入变更都让布局重新布局。
    MyBaseLayout *layout = (MyBaseLayout*)textView.superview;
    [layout setNeedsLayout];
}


@end

